package cn.sdfykjyxgs.synserver;

import cn.sdfykjyxgs.syncommon.mapper.FileChangeLogErrorMapper;
import cn.sdfykjyxgs.syncommon.service.FileChangeLogService;
import cn.sdfykjyxgs.synserver.util.FileListening;
import cn.sdfykjyxgs.synserver.util.JnotifyListening;
import cn.sdfykjyxgs.synserver.util.SpringUtil;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;
@Configuration
@MapperScan("cn.sdfykjyxgs.syncommon.mapper")
@SpringBootApplication(scanBasePackages = {"cn.sdfykjyxgs"})
public class FileSynServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(FileSynServerApplication.class, args);
        ApplicationContext context = SpringUtil.getApplicationContext();
        FileChangeLogService bean = context.getBean(FileChangeLogService.class);
        FileChangeLogErrorMapper errorMapper = context.getBean(FileChangeLogErrorMapper.class);
        Environment environment = context.getBean(Environment.class);
        String property = environment.getProperty("file.syn-path");
        //开一个线程，为了不影响主程序运行
        new Thread() {
            @Override
            public void run() {
                JnotifyListening listening = new JnotifyListening(property,bean, errorMapper);
                listening.beginWatch();

            }
        }.start();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        // 初始化 MybatisPlusInterceptor 核心插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加自动分页插件 PaginationInnerInterceptor
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 返回
        return interceptor;
    }
}
